package com.situ.day18home;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class StudentManager {
	public static void main(String[] args) {
		System.out.println("请输入要存入的学生个数");
		Scanner scanner = new Scanner(System.in);
		int num1 = scanner.nextInt();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(`name`,age,className) VALUES (?,?,?);";
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= num1; i++) {
				System.out.println("请输入第" + i + "个学生的姓名");
				String name = scanner.next();
				System.out.println("请输入第" + i + "个学生的年龄");
				int age = scanner.nextInt();
				System.out.println("请输入第" + i + "个学生的班级");
				String className = scanner.next();
				preparedStatement.setString(1, name);
				preparedStatement.setInt(2, age);
				preparedStatement.setString(3, className);
				int count = preparedStatement.executeUpdate();
			}
			System.out.println("信息输入完毕");
			System.out.println("----------------------------------");
			System.out.println("----1.查询----");
			System.out.println("----2.增加----");
			System.out.println("----3.修改----");
			System.out.println("----4.删除----");
			System.out.println("----5.退出----");
			System.out.println("请选择您要进行的操作");
			int num2 = scanner.nextInt();
			if (num2 == 5) {
				System.out.println("已退出系统");
				return;
			}
			switch (num2) {
			case 1:
				System.out.println("欢迎使用查询功能，请选择您要查询的字段：");
				System.out.println("----1.id----");
				System.out.println("----2.名字----");
				System.out.println("----3.年龄----");
				System.out.println("----4.班级----");
				int num3 = scanner.nextInt();
				switch (num3) {
				case 1:
					System.out.println("请输入id");
					int num4 = scanner.nextInt();
					String sql1 = "SELECT * FROM student WHERE id = ?;";
					preparedStatement = connection.prepareStatement(sql1);
					preparedStatement.setInt(1, num4);
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						int age = resultSet.getInt("age");
						String className = resultSet.getString("className");
						System.out.println("id:" + id);
						System.out.println("name:" + name);
						System.out.println("age:" + age);
						System.out.println("className:" + className);
					}
					break;
				case 2:
					System.out.println("请输入名字");
					String num5 = scanner.next();
					String sql2 = "SELECT * FROM student WHERE name = ?;";
					preparedStatement = connection.prepareStatement(sql2);
					preparedStatement.setString(1, num5);
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						int age = resultSet.getInt("age");
						String className = resultSet.getString("className");
						System.out.println("id:" + id);
						System.out.println("name:" + name);
						System.out.println("age:" + age);
						System.out.println("className:" + className);
					}
					break;
				case 3:
					System.out.println("请输入年龄");
					int num6 = scanner.nextInt();
					String sql3 = "SELECT * FROM student WHERE age = ?;";
					preparedStatement = connection.prepareStatement(sql3);
					preparedStatement.setInt(1, num6);
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						int age = resultSet.getInt("age");
						String className = resultSet.getString("className");
						System.out.println("id:" + id);
						System.out.println("name:" + name);
						System.out.println("age:" + age);
						System.out.println("className:" + className);
					}
					break;
				case 4:
					System.out.println("请输入班级");
					String num7 = scanner.next();
					String sql4 = "SELECT * FROM student WHERE className = ?;";
					preparedStatement = connection.prepareStatement(sql4);
					preparedStatement.setString(1, num7);
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						int age = resultSet.getInt("age");
						String className = resultSet.getString("className");
						System.out.println("id:" + id);
						System.out.println("name:" + name);
						System.out.println("age:" + age);
						System.out.println("className:" + className);
					}
					break;

				default:
					break;
				}
			case 3:
				System.out.println("欢迎使用修改功能");
				System.out.println("请输入要修改的信息对应的id");
				int num9 = scanner.nextInt();
				System.out.println("请选择需要修改该条信息的内容（只能选择一个呦）");
				System.out.println("----1.id----");
				System.out.println("----2.name----");
				System.out.println("----3.age----");
				System.out.println("----4.className----");
				int num10 = scanner.nextInt();
				switch (num10) {
				case 1:
					System.out.println("您要把这条信息的id修改为：");
					int num11 = scanner.nextInt();
					String sql15 = "UPDATE student SET id = ? WHERE id = ?;";
					preparedStatement = connection.prepareStatement(sql15);
					preparedStatement.setInt(1, num11);
					preparedStatement.setInt(2, num9);
					int count = preparedStatement.executeUpdate();
					System.out.println("修改成功");
					break;
				case 2:
					System.out.println("您要把这条信息的名字修改为：");
					String num17 = scanner.next();
					String sql16 = "UPDATE student SET name = ? WHERE id = ?;";
					preparedStatement = connection.prepareStatement(sql16);
					preparedStatement.setString(1, num17);
					preparedStatement.setInt(2, num9);
					int count1 = preparedStatement.executeUpdate();
					System.out.println("修改成功");
					break;
				case 3:
					System.out.println("您要把这条信息的年龄修改为：");
					int num18 = scanner.nextInt();
					String sql17 = "UPDATE student SET age = ? WHERE id = ?;";
					preparedStatement = connection.prepareStatement(sql17);
					preparedStatement.setInt(1, num18);
					preparedStatement.setInt(2, num9);
					int count2 = preparedStatement.executeUpdate();
					System.out.println("修改成功");
					break;
				case 4:
					System.out.println("您要把这条信息的班级修改为：");
					int num19 = scanner.nextInt();
					String sql18 = "UPDATE student SET age = ? WHERE id = ?;";
					preparedStatement = connection.prepareStatement(sql18);
					preparedStatement.setInt(1, num19);
					preparedStatement.setInt(2, num9);
					int count3 = preparedStatement.executeUpdate();
					System.out.println("修改成功");
					break;
				default:
					break;
				}
				
				break;
				
				
			case 4:
				System.out.println("欢迎使用删除功能");
				System.out.println("因为开发局限，请输入您需要删除信息的对应id");
				int num8 = scanner.nextInt();
				String sql5 ="DELETE FROM student WHERE id = ?;";
				preparedStatement = connection.prepareStatement(sql5);
				preparedStatement.setInt(1, num8);
				int count = preparedStatement.executeUpdate();
				System.out.println("恭喜您！删除成功！！");
				break;

			default:
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
